﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Terminal;
using Senparc.Weixin.Helpers.Extensions;
using Senparc.Weixin.Work.AdvancedAPIs;
using Senparc.Weixin.Work.AdvancedAPIs.MailList;
using Senparc.Weixin.Work.Containers;
using WeiXinAPI.Common;
using WeiXinAPI.model;

namespace WeiXinAPI
{
    public class WxMsgTemplateHandler : BaseHandler
    {
        #region 添加消息模板 AddMsgTemplate

        [JsonType]
        [MustLogin]
        [Description("添加消息模板")]
        [ReturnType(typeof(int))]
        public ApiResult AddMsgTemplate(AutoSend_MsgTemplate msgTemplate)
        {
            var dc = new TerminalEntities();
            msgTemplate.Enable = true;
            dc.AutoSend_MsgTemplate.Add(msgTemplate);
            var result = dc.SaveChanges();
            return Success(result);
        }

        #endregion

        #region 修改消息模板 UpdateMsgTemplate

        [JsonType]
        [MustLogin]
        [Description("修改消息模板")]
        [ReturnType(typeof(int))]
        public ApiResult UpdateMsgTemplate(AutoSend_MsgTemplate msgTemplate)
        {
            var dc = new TerminalEntities();
            msgTemplate.Enable = true;
            dc.AutoSend_MsgTemplate.AddOrUpdate(msgTemplate);
            var result = dc.SaveChanges();
            return Success(result);
        }

        #endregion

        #region 获取消息模板 GetMsgTemplate

        [MustLogin]
        [Description("获取消息模板")]
        [ReturnType(typeof(int))]
        public ApiResult GetMsgTemplate(int Id)
        {
            if (Id <= 0)
            {
                return Error("参数异常，请重新再试");
            }

            var dc = new TerminalEntities();
            var model = dc.AutoSend_MsgTemplate.FirstOrDefault(x => x.Id == Id && x.Enable);
            if (model != null)
            {
                return Success(model);
            }
            return Error("找不到对应的模板信息，请重新再试");
        }

        #endregion

        #region 删除消息模板 RemoveMsgTemplate

        [MustLogin]
        [Description("删除消息模板")]
        [ReturnType(typeof(int))]
        public ApiResult RemoveMsgTemplate(int Id)
        {
            if (Id <= 0)
            {
                return Error("参数异常，请重新再试");
            }

            var dc = new TerminalEntities();
            var model = dc.AutoSend_MsgTemplate.FirstOrDefault(x => x.Id == Id && x.Enable);
            if (model != null)
            {
                model.Enable = false;
                var result = dc.SaveChanges();
                return Success(result);
            }
            return Error("删除失败！找不到对应的模板信息");
        }

        #endregion

        #region 获取自动发送配置列表 GetAutoSendMsgTemplateList

        [MustLogin]
        [Description("获取自动发送配置列表")]
        [ReturnType(typeof(IList<DepartmentListForJsTree>))]
        public ApiResult GetAutoSendMsgTemplateList()
        {
            var dc = new TerminalEntities();
            var list = dc.AutoSend_MsgTemplate.Where(x => x.Enable).ToList();
            return Success(list);
        }

        #endregion

        #region 设置自动发送信息 SetAutoSendMsgSetting

        [JsonType]
        [MustLogin]
        [Description("设置自动发送信息")]
        [ReturnType(typeof(int))]
        public ApiResult SetAutoSendMsgSetting(AutoSendMsgInfo infos)
        {
            var wxInfo = WXBaseHandler.TryGetWxCorpInfo(infos.WxConfig);
            if (wxInfo.CorpInfo == null)
            {
                return Error(wxInfo.Message);
            }

            var dc = new TerminalEntities();
            var uids = string.Join("|", infos.MemberIdList);

            var obj = dc.AutoSend_Setting.FirstOrDefault(x => x.Id == infos.Id);
            if (obj == null)
            {
                var model = new AutoSend_Setting()
                {
                    CIdentification = wxInfo.CorpInfo.CIdentification,
                    ReportId = infos.ReportId,
                    TemplateId = infos.MsgTemplateId,
                    UserIds = uids
                };
                dc.AutoSend_Setting.Add(model);
            }
            else
            {
                obj.CIdentification = wxInfo.CorpInfo.CIdentification;
                obj.ReportId = infos.ReportId;
                obj.TemplateId = infos.MsgTemplateId;
                obj.UserIds = uids;
            }
            var result = dc.SaveChanges();
            return Success(result);
        }

        #endregion

        

    }
}
